home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_100 / 104_01 / time.c < prev    next >
Text File  |  1980-01-01  |  5KB  |  203 lines

  1. /********************************************************/
  2. /*                            */
  3. /*    copyright March ,1981 By Mike Bernson        */
  4. /*                            */
  5. /*    program used to set and display time on QT    */
  6. /*    s-100 clock calendar board            */
  7. /*                            */
  8. /*    command format:                    */
  9. /*    time [hh:mm:ss] [am,pm] [mm/dd/yy] [day]    */
  10. /*                            */
  11. /********************************************************/
  12.  
  13. #define CONTROL    129        /* address of control port */
  14. #define ADDRESS 129         /* address port on clock board */
  15. #define DATA    130         /* data port for clock board */
  16. #define HOLD    0x10        /* bit for hold line on clock */
  17. #define READ    0x20        /* bit for read line on clock */
  18. #define WRITE    0x10        /* bit for write line on clock */
  19. #define TRUE    1
  20. #define FALSE    0
  21.  
  22. main(argc,argv)
  23. int argc;
  24. char *argv[];
  25. {
  26.     char string[80],*check;
  27.     int  am;
  28.  
  29.     /* if no arg on command line display time */
  30.     if (argc==1) {
  31.         puts("Current time is ");
  32.         puts(time_display(string));
  33.         exit();
  34.         }
  35.  
  36.     am=-1;
  37.     check=argv[2];
  38.  
  39.     if (argc==3) {
  40.         if (isalpha(check[0])) {
  41.             if (check[0]=='A') am=0;
  42.             if (check[0]=='P') am=1;
  43.             if (am != -1 && (check[1] !='M' || check[2] != 0)) {
  44.                 puts("Bad command line must be ");
  45.                 puts(" (HH:MM [AM || PM]) [MM/DD/YY]");
  46.                 exit();
  47.                 }
  48.             }
  49.         }
  50.  
  51.     /* set time value from input */
  52.     if (time_set(argv[1],am)) exit();
  53.     puts("\nTime set to ");puts(time_display(string));
  54.     exit();
  55.     }
  56.  
  57. /********************************************************/
  58. /*                            */
  59. /*    time_display                    */
  60. /*                            */
  61. /*    function:    to build a string for current    */
  62. /*            time in format hh:mm:ss [am,pm] */
  63. /*                            */
  64. /*                            */
  65. /*    input:        string to put current time in    */
  66. /*                            */
  67. /*    return:        pointer to input string        */
  68. /*                            */
  69. /*    date written:    March 8, 1981 By Mike Bernson    */
  70. /*                            */
  71. /********************************************************/
  72. time_display(string)
  73. char *string;
  74. {
  75.     char clock[6],*temp1;
  76.     int  temp,am;
  77.  
  78.     /* set hold on clock and read clock time */
  79.     outp(CONTROL,HOLD);
  80.     temp=-1;
  81.     while(++temp<6) {
  82.         outp(DATA,temp | READ);
  83.         clock[temp]=inp(DATA);
  84.         }
  85.     outp(CONTROL,0);
  86.  
  87.     am=-1;
  88.     if ((clock[5] & 8) == 0) am=(clock[5] & 4)/4;
  89.     clock[5] &= 3;
  90.     
  91.     /* convert time to printable string */
  92.     temp1=string;
  93.     temp=6;
  94.     while(--temp >= 0 ) {
  95.         if (temp==1 || temp==3) *temp1++=':';
  96.         *temp1++=clock[temp]+'0';
  97.         }
  98.     /* add am or pm if needed */
  99.     switch(am) {
  100.  
  101.         case -1 :
  102.             strcpy(temp1," ");
  103.             break;
  104.  
  105.         case 0 :
  106.             strcpy(temp1," AM ");
  107.             break;
  108.  
  109.         case 1 : 
  110.             strcpy(temp1," PM ");
  111.             break;
  112.         }
  113.     return string;
  114.     }
  115.  
  116. /********************************************************/
  117. /*                            */
  118. /*    time_set                    */
  119. /*                            */
  120. /*    function:    to set the time from input     */
  121. /*                            */
  122. /*    input:        string that has time to set    */
  123. /*            am/pm/24 switch            */
  124. /*                            */
  125. /*    date_written:    Mrach 8, 1981 By Mike Bernson    */
  126. /*                            */
  127. /********************************************************/
  128. time_set(string,am)
  129. char *string;
  130. int  am;
  131. {
  132.     char clock[13];
  133.     int  temp;
  134.     
  135.     /* build array to send to clock for time */
  136.     for(temp=5; temp>=2; temp--) {
  137.         if (temp==5 && string[1] == ':') {
  138.             clock[5]=0;
  139.             continue;
  140.             }
  141.         if (temp==3)
  142.             if (*string++ != ':') {
  143.                 puts("Bad time format missing \":\"");
  144.                 puts("(HH:MM [AM || PM])");
  145.                 return TRUE;
  146.                 }
  147.         if (!isdigit(*string)) {
  148.                  puts("Bad digit in time format");
  149.                 return TRUE;
  150.                 }
  151.         clock[temp]=(*string++) & 0x0f;
  152.         }
  153.     clock[1]=clock[0]=0;
  154.  
  155.     if (*string !=0) {
  156.         puts("Bad time format too many digits (HH:MM [AM || PM])");
  157.         return TRUE;
  158.         }
  159.     
  160.     /* check to see that ten second and tens of hours less then 7 */
  161.     if (clock[3]>5) {
  162.         puts("Bad tens of minutes digit");
  163.         return TRUE;
  164.         }
  165.  
  166.     /* check hours dights */
  167.     temp=clock[5]*10+clock[4];
  168.     if ((temp<1 || temp>24) && am<0) {
  169.         puts("Bad hours range is 1-24 only");
  170.         return TRUE;
  171.         }
  172.     if ((temp<1 || temp>12) && am>=0) {
  173.         puts("Bad hours range is 1-12 only");
  174.         return TRUE;
  175.         }
  176.     /* set 24 hour mode and am/pm bits */
  177.     if (am<0) clock[5] |= 8;
  178.     else clock[5] |= am * 4;
  179.  
  180.     /* wait for user to hit return before setting time */
  181.     puts("Hit any key to set clock ");bios(3,0);
  182.  
  183.     /* send data to clock */
  184.     for(temp=5; temp>=0; --temp) {
  185.         outp(DATA,temp);
  186.         outp(ADDRESS,clock[temp] | HOLD);
  187.         outp(DATA,temp | WRITE);
  188.         outp(DATA,temp);
  189.         }
  190.     outp(CONTROL,0);
  191.     return FALSE;
  192.     }
  193.  
  194. 
  195.     }
  196.  
  197. 
  198.  
  199. LSE;
  200.     }
  201.  
  202. 
  203. 8δ)8δ))8δ))